package com.xuecheng.manage_cms.web.controller;

import com.xuecheng.api.cms.CmsPageControllerApi;
import com.xuecheng.framework.domain.cms.CmsPage;
import com.xuecheng.framework.domain.cms.request.QueryPageResult;
import com.xuecheng.framework.domain.cms.response.CmsPageResult;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.manage_cms.service.PageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Andrew Yang
 * @ClassName CmsPageController
 * @Description
 * @Package com.xuecheng.manage_cms.web.controller
 * @create 2019-12-25 14:35
 */
@Api(value = "cms页面管理接口",
        tags = "cms页面管理接口，提供页面的增、删、改、查")
@RestController
@RequestMapping("/page")
public class CmsPageController implements CmsPageControllerApi {
    @Autowired
    private PageService pageService;
    /**
     * @param page
     * @param size
     * @param queryPageResult
     * @Method: findList
     * @Author: Andrew Yang
     * @Date: 2019/12/25 14:44
     * @Return com.xuecheng.framework.model.response.QueryResponseResult
     * @Description: 通用查询方法
     */
    @Override
    @GetMapping("/list/{page}/{size}")
    @ApiOperation("分页查询页面列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",
                    value = "页码",
                    required = true,
                    paramType = "path",
                    dataType = "int"),

            @ApiImplicitParam(name="size",
                    value = "每页记录数",
                    required = true,
                    paramType = "path",
                    dataType = "int")
    })
    public QueryResponseResult findList(@PathVariable("page") int page,
                                        @PathVariable("size") int size,

                                        QueryPageResult queryPageResult) {
        return pageService.findList(page,
                size,
                queryPageResult);
    }

    @PostMapping("/add")
    @Override
    public CmsPageResult add(@RequestBody CmsPage cmsPage) {
        return pageService.add(cmsPage);
    }

    @ApiOperation("通过ID查询页面")
    @GetMapping("/findById/{id}")
    @Override
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",
                    value = "id",
                    required = true,
                    paramType = "path",
                    dataType = "String"),
    })
    public CmsPageResult findById(@PathVariable("id") String id) {
        if (StringUtils.isEmpty(id)) {
            return new CmsPageResult(CommonCode.FAIL, "未勾选id，不可查询数据", null);
        }
        return pageService.findById(id);
    }
    @ApiOperation("根据id更新数值")
    @PutMapping("/edit/{id}")
    @Override
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",
                    value = "id",
                    required = true,
                    paramType = "path",
                    dataType = "String"),
            @ApiImplicitParam(
                    name="cmsPage",
                    required = true,
                    paramType = "body",
                    dataType = "Object"
            )
    })
    public CmsPageResult edit(@PathVariable("id")String id, @RequestBody CmsPage cmsPage) {
        return pageService.edit(id, cmsPage);
    }

    @ApiOperation("通过ID删除页面")
    @DeleteMapping("/del/{id}")
    @Override
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",
                    value = "id",
                    required = true,
                    paramType = "path",
                    dataType = "String"),
    })
    public ResponseResult delete(@PathVariable("id")String id) {
        return pageService.delete(id);
    }

    @ApiOperation("发布页面")
    @Override
    @PostMapping("/postPage/{pageId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageId",
                    value = "pageId",
                    required = true,
                    paramType = "path",
                    dataType = "String"),
    })
    public ResponseResult post(@PathVariable("pageId") String pageId) {
        return pageService.postPage(pageId);
    }
}
